<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input
          style="margin-bottom:10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
        />
        <!-- 树形组件 -->
        <el-tree
          ref="deptTree"
          node-key="id"
          :data="data"
          :props="defaultProps"
          default-expand-all
          :expand-on-click-node="false"
          highlight-current
          @current-change="selectNode"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button size="mini">excel导入</el-button>
          <el-button size="mini">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->

        <el-table :data="getDepartmentList">
          <el-table-column prop="staffPhoto" align="center" label="头像">
            <template v-slot="{ row }">
              <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" :size="30" />
              <span v-else class="username">{{ row.username?.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" label="聘用形式" :formatter="formatter" />
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作" width="280px">
            <template>
              <el-button size="mini" type="text">查看</el-button>
              <el-button size="mini" type="text">角色</el-button>
              <el-button size="mini" type="text">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-pagination
          :page-size="params.pagesize"
          layout="prev, pager, next"
          :total="total"
          @current-change="currentchange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { getDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils/index'
import { getEmployeeList } from '@/api/employee'

export default {
  name: 'Employee',
  data() {
    return {
      data: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      params: {
        page: 1,
        pagesize: 5,
        departmentId: null
      },
      total: 0,
      getDepartmentList: []
    }
  },
  created() {
    this.getDepartment()
    this.getEmployee()
  },
  methods: {
    async getDepartment() {
      const res = await getDepartment()
      console.log(res)
      this.data = transListToTreeData(res, 0)
      this.params.departmentId = this.data[0].id

      this.$refs.deptTree.setCurrentKey(this.params.departmentId)
    },
    selectNode(node) {
      this.params.departmentId = node.id
      this.getEmployee()
    },
    async getEmployee() {
      const res = await getEmployeeList(this.params)
      console.log(res.rows)
      this.getDepartmentList = res.rows
      this.total = res.total
    },
    // 分页器
    currentchange(page) {
      this.params.page = page
      this.getEmployee()
    },
    // 格式化
    formatter(type) {
      const API = {
        1: '正式',
        2: '非正式'
      }
      return API[type.formOfEmployment]
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;

  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }

  .right {
    flex: 1;
    padding: 20px;

    .opeate-tools {
      margin: 10px;
    }

    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display: inline-block;
    }
  }
}
</style>
